package com.itsu.app.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsu.app.exception.MyException;
import com.itsu.app.interceptor.UserThreadLocal;
import com.itsu.interfaces.entity.UserInfoEntity;
import com.itsu.interfaces.service.UserInfoService;
import com.itsu.pojo.constants.Constants;
import com.itsu.pojo.dto.RecommendationDTO;
import com.itsu.pojo.dto.UserInfoDTO;
import com.itsu.pojo.enums.ResultCode;
import com.itsu.pojo.results.ErrorResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.itsu.commons.utils.FaceAiUtil;
import com.itsu.commons.utils.OssUtil;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class AppUserInfoService {

    @Reference
    UserInfoService userInfoService;


    //
    public void loginReginfo(UserInfoDTO userInfoDTO) {

        Long uid = UserThreadLocal.getUid();

        Optional<UserInfoDTO> optional = Optional.ofNullable(userInfoDTO);
        if (!optional.isPresent()) {
            throw new MyException(new ErrorResult(ResultCode.PARAM_INVALID));
        }

        //todo 校验用户信息是否已添加过了

        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setStringSex(userInfoDTO.getGender());
        userInfoEntity.setNickName(userInfoDTO.getNickname());
        userInfoEntity.setBirthday(userInfoDTO.getBirthday());
        userInfoEntity.setCity(userInfoDTO.getCity());
        userInfoEntity.setUserId(uid);
        userInfoEntity.setCreated(new Date());
        userInfoService.addUserInfo(userInfoEntity);
    }

    public void loginReginfoHead(MultipartFile headPhoto) {

        Long uid = UserThreadLocal.getUid();


        // 校验文件的格式
        // png  jpg jpeg  abcpng
        if (!StrUtil.endWithAnyIgnoreCase(headPhoto.getOriginalFilename(), ".png", ".jpg", ".jpeg")) {
            throw new MyException(new ErrorResult(ResultCode.PHOTO_FORMART_ERROR));
        }
        // 校验文件的大小
        if ((headPhoto.getSize() / 1024 / 1024) >= 2) {
            throw new MyException(new ErrorResult(ResultCode.PHOTO_SIZE_ERROR));
        }


        // 校验人脸
        try {
            if (!FaceAiUtil.aiFace(headPhoto.getBytes())) {
                throw new MyException(new ErrorResult(ResultCode.PHOTO_NAN_ERROR));
            }
            // 上传到阿里云OSS  返回地址
            String pictureUrl = OssUtil.saveImage(headPhoto.getBytes(),
                   StrUtil.subAfter(headPhoto.getOriginalFilename(), ".", true));
            // 把地址更新到MYSQL数据库
            userInfoService.updateUserLogo(UserInfoEntity.builder().userId(uid).logo(pictureUrl).build());
        } catch (IOException e) {
            throw new MyException(new ErrorResult(ResultCode.PHOTO_FORMART_ERROR));
        }

    }

    /**
     * 根据用户Id查询用户信息对象
     * @param uid
     * @return
     */
    public UserInfoEntity findUserByUid(Long uid) {
        return userInfoService.findUserByUid(uid);
    }


    /**
     * 根据推荐人的用户ID、分页信息、查询条件查询用户信息对象
     * @param userIds
     * @param recommendationDTO
     * @return
     */
    public Page<UserInfoEntity> findUserListOfRecommendation(List<Long> userIds, RecommendationDTO recommendationDTO) {
        return userInfoService.findUserListOfRecommendation(userIds, recommendationDTO);
    }

    public Page<UserInfoEntity> pageUsers(Integer page, Integer pageSize, List<Long> userIds) {
        return  userInfoService.pageUsers(page, pageSize, userIds);
    }

    public Page<UserInfoEntity> pageUsers(Integer page, Integer pageSize, List<Long> userIds, String keyword) {
        return  userInfoService.pageUsers(page, pageSize, userIds, keyword);
    }

    public List<UserInfoEntity> findUserByUids(List<Long> userIds) {
       return userInfoService.findUserByUids(userIds);
    }

    public void updateInfo(UserInfoEntity entity) {
        userInfoService.updateUserInfo(entity);
    }

    public void save(UserInfoEntity userInfoEntity) {
        userInfoService.addUserInfo(userInfoEntity);
    }
}
